<?php
if (!defined("APP_PATH")) {
    define('APP_PATH',__DIR__.'/../application/');
    // 加载框架引导文件
    require  __DIR__.'/../thinkphp/start.php';
}
echo "<pre>";
//每60分钟触发一次
//if(config("site.jackpot_switch")) jackpot();
//TODO 弃用
function jackpot()
{
    $debug = config("app_debug");
    if (php_sapi_name() != 'cli' && !$debug) {
        echo '只能在cli模式下或者开启调试模式运行：' . date('Y-m-d H:i:s') . "\r\n";die;
    }

    echo '开始执行的时间：'.date('Y-m-d H:i:s')."\r\n";
    $redis = rds();
    $bkey = 'jackpot';
    if($debug) delkey($redis,$bkey);
    $redis->del(date('Ymd',mktime(0,0,0,date('m'),date('d')-7,date('Y'))).'_sy');//清除7天前的会员每日收益
    $op = $redis->set($bkey,1,['NX','EX'=>3300]);
    if(!$op){
        echo '请勿频繁操作：'.date('Y-m-d H:i:s')."\r\n";
        goto END;
    }

    $kc_balance = \db('config')->where('name','kc_balance')->value('value');
    if($kc_balance > 0){
        $d = date('Ymd');
        $bi = $kc_balance*config("site.jackpot_bi")/100;//奖池参数
        $bi = $bi>0.1?0.1:$bi;//最高封顶10%
        $users = \db('user')->where(['credit1'=>['>',0]])->field('id,credit1')->select();
//        $lua = '';
        //顺便校验下缓存中投资金额是否正确
        foreach($users as $user){
            $add_sy = $user['credit1']*$bi/24;
            if($add_sy <= 0) continue;
            echo $user['id'].'--奖池收益:'.$add_sy.">>>";
            $lua = <<<LUA
            local user_id = KEYS[1]
            local day_sy_key = KEYS[2]
            local add_sy = ARGV[1]
            local credit1 = ARGV[2]
            local tz = redis.call('hGet','user_tz',user_id)
            if tz == false or tz ~= credit1
            then
                redis.call('hSet','user_tz',user_id,credit1)
            end

            local vel = redis.call('hGet','user_tz',user_id)
            local day_sy = redis.call('hGet',day_sy_key,user_id)
            if day_sy == false
            then
                redis.call('hIncrByFloat','user_sy',user_id,add_sy)
                redis.call('hIncrByFloat',day_sy_key,user_id,add_sy)
            else
                local new_sy = day_sy + add_sy
                local max_sy = vel*0.1
                day_sy = day_sy*1
                if day_sy < max_sy
                then
                    if new_sy <= max_sy
                    then
                        redis.call('hIncrByFloat','user_sy',user_id,add_sy)
                        redis.call('hIncrByFloat',day_sy_key,user_id,add_sy)
                    else
                        local add_sy = max_sy - day_sy
                        redis.call('hIncrByFloat','user_sy',user_id,add_sy)
                        redis.call('hSet',day_sy_key,user_id,max_sy)
                    end
                end
            end
            return true
LUA;
            $res = $redis->eval($lua,['user_'.$user['id'],$d.'_sy',$add_sy,$user['credit1']],2);//如果执行失败就会返回false
//            var_dump($res);
            if($res === false){
                $redis->hIncrByFloat('error_sy','user_'.$user['id'],$add_sy);
                \think\Log::error('奖池奖励：'.$user['id'].' += '.$add_sy.'失败');
                echo '奖池收益：'.$user['id'].' += '.$add_sy.'失败!!!'."\r\n";
            }
        }
        unset($users);
//        if($lua) $lua .= " return true";
//        $ret = $redis->eval($lua);
////        var_dump($ret,$redis->hGetAll('user_tz'),$redis->hGetAll('user_sy'));
//        if($ret === false){
//            \think\Log::error("结算总资奖池奖励失败");
//            echo '执行失败：'.date('Y-m-d H:i:s')."\r\n";
//        }
    }else{
        echo '矿池余额为0：'.date('Y-m-d H:i:s')."\r\n";
    }

    delkey($redis,$bkey);
    END:
    echo "\r\n".'结束执行的时间：'.date('Y-m-d H:i:s')."\r\n";
}


